const Store = require("electron-store");
const store = new Store();

export default {
  methods: {
    /**
     *
     * @param name
     * @param value
     * @returns {string}
     */
    storage(name, value) {
      if (!value) {
        return store.get(name);
      }
      store.set(name, value);
    },

    initTableWidth(columns, storageName) {
      const storageTable = this.storage(storageName, null) || {};
      Object.keys(storageTable).forEach((key) => {
        const col = this.findColumnByProp(columns, key);
        col.width = storageTable[key];
      });
    },

    onHeaderDragend(currenWidth, oldWidth, el, storageName) {
      const { property } = el;
      const storageTable = this.storage(storageName, null) || {};
      console.log("storageTable1:", storageTable);
      storageTable[property] = currenWidth;
      console.log("storageTable2:", storageTable);
      this.storage(storageName, storageTable);
    },
    findColumnByProp(columns, prop) {
      const column = columns.filter((item) => item.prop === prop);
      return column.length ? column[0] : null;
    },
  },
};
